package com.chineseall.rebirth.bridge.app;

import java.io.IOException;

import jmind.core.util.DataUtil;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class AppEncrypt {

    public static void main(String[] args) {
        System.out.println(generateToken(15756917));
    }

    /** 私钥 加密uid**/
    public static final String secretKey = "hd1ej62@";

    public static int getUserIdByToken(String token) {
        String[] keys = unToken(token);
        if (null != keys && keys.length == 3) {
            return DataUtil.toInt(keys[2]);
        }
        return 0;
    }

    /**
     * @author 陈联聪 Cesar
     * @version 1.0
     * @param secret
     *            双方协定的连接密码
     * @param key
     *            用户名
     * @return 返回 [双方协定的连接密码]+[:]+[系统时间]+[:]+[用户名]
     * @throws Exception
     */
    public static String generateToken(int uid) {
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        StringBuilder sb = new StringBuilder();
        sb.append(secretKey).append(":");
        sb.append(time).append(":");
        sb.append(uid);
        String token = null;
        try {
            token = encrypt(sb.toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return token;
    }

    /**
     * <dl>
     * <dt>unPar</dt>
     * <dd>TODO 2007-3-21 下午04:08:52</dd>
     * <dd>Description:将加密参数进行解密,并返回</dd>
     * </dl>
     * 
     * @author 陈联聪 Cesar
     * @version 1.0
     * @param token
     *            加密的参数
     * @return 解密后的参数
     * @throws IOException
     */
    private static String[] unToken(String token) {
        String str = unEncrypt(token);
        if (str.isEmpty())
            return null;
        return str.split(":");
    }

    /**
     * <dl>
     * <dt>encrypt</dt>
     * <dd>TODO 2007-3-21 下午03:40:19</dd>
     * <dd>Description:对字符串进行BASE64加密</dd>
     * </dl>
     * 
     * @author 陈联聪 Cesar
     * @version 1.0
     * @param str
     *            需加密的字符串
     * @return 返回加密后的字符串
     * @throws Exception
     */
    private static String encrypt(String str) throws Exception {
        if (DataUtil.isEmpty(str)) {
            return DataUtil.EMPTY;
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(str.getBytes());
    }

    /**
     * <dl>
     * <dt>unEncrypt</dt>
     * <dd>TODO 2007-3-21 下午03:41:36</dd>
     * <dd>Description:对BASE64加密后的字符串进行解密</dd>
     * </dl>
     * 
     * @author 陈联聪 Cesar
     * @version 1.0
     * @param str
     *            需解密的字符串
     * @return 返回解密后的字符串
     * @throws IOException
     */
    private static String unEncrypt(String str) {
        if (DataUtil.isEmpty(str)) {
            return DataUtil.EMPTY;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = null;
        try {
            b = decoder.decodeBuffer(str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new String(b);
    }

}